package others;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;


public class Try {

	public static class ListNode{

    public int value;

    public ListNode next;
 }

 public static boolean hasLoops( ListNode myList ) {
     
     if(myList == null) return false;//check input
     
     ListNode first = myList, second = myList; //start at the same point
     
     while(true){
         
         first = first.next;
         
         if(second.next != null) second = second.next.next;
         else return false;//null node means no loop
         
         
         if(first == null || second == null) return false;//null node
         
         if(first == second) return true;//two indicators meet each other
         
     }

 }
 
 
 Map <Integer, Double> calculateFinalScores (List<TestResult> results) {
     
     if(results == null) return null;
     
     HashMap<Integer, PriorityQueue<Integer>> map = new HashMap<Integer, PriorityQueue<Integer>>();
     
     for(TestResult res : results){
         if(map.containsKey(res.studentId)){
             PriorityQueue<Integer> q = map.get(res.studentId);
             q.add(res.testScore);
         }else{//doesn't contain this student
             PriorityQueue<Integer> q = new PriorityQueue<Integer>(5,
                                                      new Comparator<Integer>(){
			        @Override
			        public int compare(Integer o1, Integer o2) {
				        if(o1 > o2) return -1;
				        if(o1 < o2) return 1;
				        return 0;
			        }
		            });
             
             q.add(res.testScore);
             map.put(res.studentId, q);
         }
     }
     
     Map<Integer, Double> result = new HashMap<Integer, Double>();
     
     for(Entry<Integer, PriorityQueue<Integer>> ent : map.entrySet()){
         
         Double score = 0.0;
         
         PriorityQueue<Integer> q = ent.getValue();
         
         for(int i = 0 ; i < 5 ; i++) score += q.poll();
         
         result.put(ent.getKey(), (score / 5.0));
     }
     
     return result;
 }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		
		HashMap<Integer, List<String>> map = new HashMap<Integer, List<String>>();
		map.put(1, new ArrayList<String>());
		map.get(1).add("ouhang");
		System.out.println();
		main(args);
	}

}

class TestResult {
    int studentId;
    String testDate;
    int testScore;
 }
